/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef PMC_REG_ACCESS_H
#define PMC_REG_ACCESS_H

/*!
 * @file pmc_reg_access.h
 * @brief This file declares or defines pmc register access functions
 */

/*******Includes***************************************************************/
#include "pmc_drv.h"

/*******Definitions************************************************************/
#define PMC_CR_LVD_EN_VLD(x) (((uint32_t)(((uint32_t)(x)) << PMC_CR_LVDEN_Pos)) & PMC_CR_LVDEN_Msk)
#define PMC_CR_LVD_INT_VLD(x) (((uint32_t)(((uint32_t)(x)) << PMC_CR_LVDIE_Pos)) & PMC_CR_LVDIE_Msk)

#if CONFIG_NMI_INT_WAKEUP_ENABLE_INDEPENDENT_FEATURE
#define PMC_CR_NMI_INT_VLD(x) (((uint32_t)(((uint32_t)(x)) << PMC_CR_NMIIE_Pos)) & PMC_CR_NMIIE_Msk)
#endif
/*******APIs*******************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief LVD enable
 *
 * @param[in] regBase: The PMC register base address
 * @param[in] enable: Enable State
 *            - true: Enable the LVD module
 *            - false: Disable the LVD module
 * @return None
 */
static inline void LVD_REG_SetEnable(PMC_Type* regBase, bool enable)
{
    regBase->CR = (regBase->CR & ~PMC_CR_LVDEN_Msk) | PMC_CR_LVD_EN_VLD(enable ? 1U : 0U);
}

/*!
 * @brief Set LVD level high or low
 *
 * @param[in] regBase: The PMC base address
 * @param[in] lvdLevel: LVD level defined in pmc_lvd_level_t
 * @return None
 */
static inline void LVD_REG_SetLevel(PMC_Type* regBase, pmc_lvd_level_t lvdLevel)
{
    uint32_t regValue = regBase->CR;

    if (lvdLevel == LVD_LEVEL_HIGH) {
        regValue &= ~PMC_CR_LVDLEVEL_Msk;
    } else {
        regValue |= PMC_CR_LVDLEVEL_Msk;
    }

    regBase->CR = regValue;
}

/*!
 * @brief LVD interrupt enable
 *
 * @param[in] regBase: The PMC register base address
 * @param[in] enable: Enable State
 *            - true: Enable the LVD interrupt
 *            - false: Disable the LVD interrupt
 * @return None
 */
static inline void LVD_REG_SetIntEnable(PMC_Type* regBase, bool enable)
{
    uint32_t regValue = regBase->CR;
    regBase->CR = (regValue & ~PMC_CR_LVDIE_Msk) | PMC_CR_LVD_INT_VLD(enable ? 1UL : 0UL);
}

/*!
 * @brief Get Low Voltage Detet or Wakeup Flag
 *
 * @param[in] regBase: The PMC register base address
 * @param[in] flag: flag defined in pmc_flag_t
 * @return  flag status:
 *          - true: flag detected
 *          - false: flag not detected
 */
static inline bool PMC_REG_GetFlag(PMC_Type* regBase, pmc_flag_t flag)
{
    uint32_t regValue = regBase->CR;

    if (flag == PMC_FLAG_LVD_WARINING) {
        regValue = (regValue & PMC_CR_LVDF_Msk) >> PMC_CR_LVDF_Pos;
    } else {
#if CONFIG_PMC_WAKEUP_FLAG_FEATURE
        regValue = (regValue & PMC_CR_WKUPF_Msk) >> PMC_CR_WKUPF_Pos;
#endif
    }

    return (regValue == 1U) ? true : false;
}

/*!
 * @brief Clear wakeup Flag
 *
 * @param[in] regBase: The PMC register base address
 * @param[in] flag: flag defined in pmc_flag_t
 * @return None
 */
static inline void PMC_REG_ClearFlag(PMC_Type* regBase, pmc_flag_t flag)
{
    uint32_t regValue;

    regValue = regBase->CR;

#if CONFIG_PMC_WAKEUP_FLAG_FEATURE

    if (flag == PMC_FLAG_LVD_WARINING) {
        regValue |= PMC_CR_LVDF_Msk;
        regValue &= ~PMC_CR_WKUPF_Msk;
    } else {
        regValue |= PMC_CR_WKUPF_Msk;
        regValue &= ~PMC_CR_LVDF_Msk;
    }

#else
    regValue |= PMC_CR_LVDF_Msk;
#endif

    regBase->CR = regValue;
}

/*!
 * @brief Set system power mode
 *
 * @param[in] regBase: The PMC base address
 * @param[in] powerMode: Power mode defined in pmc_power_mode_t
 * @return None
 */
static inline void PMC_REG_SetPowerMode(PMC_Type* regBase, pmc_power_mode_t powerMode)
{
    uint32_t regValue = regBase->CR;

    regValue &= ~(PMC_CR_LVDF_Msk);
    regValue &= ~(PMC_CR_POWERMODE_Msk);
    regValue |= ((uint32_t)powerMode << PMC_CR_POWERMODE_Pos) & PMC_CR_POWERMODE_Msk;

    regBase->CR = regValue;
}

/*!
 * @brief Set wakeup source enable
 *
 * @param[in] regBase: The PMC base address
 * @param[in] wakeupSource: Power mode defined in pmc_wakeup_source_t
 * @param[in] enable: Enable State
 *            - true: Enable wake up source
 *            - false: Disable wake up source
 * @return None
 */
static inline void PMC_REG_SetWakeupEnable(PMC_Type* regBase, pmc_wakeup_source_t wakeupSource, bool enable)
{
    if (enable == true) {
        regBase->PWE |= (1U << (uint32_t)wakeupSource);
    } else {
        regBase->PWE &= ~(1U << (uint32_t)wakeupSource);
    }
}

/*!
 * @brief NMI interrupt enable
 *
 * @param[in] regBase: The PMC register base address
 * @param[in] enable: Enable State
 *            - true: Enable the NMI interrupt
 *            - false: Disable the NMI interrupt
 * @return None
 */
#if CONFIG_NMI_INT_WAKEUP_ENABLE_INDEPENDENT_FEATURE
static inline void NMI_REG_SetIntEnable(PMC_Type* regBase, bool enable)
{
    uint32_t regValue = regBase->CR;
    regBase->CR = (regValue & ~PMC_CR_NMIIE_Msk) | PMC_CR_NMI_INT_VLD(enable ? 1UL : 0UL);
}
#endif

#if defined(__cplusplus)
}
#endif

#endif /* PMC_REG_ACCESS_H */

/*******EOF********************************************************************/
